package org.asociacion.backend.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.transaction.Transactional;

import org.asociacion.backend.bo.IndexImageBo;
import org.asociacion.backend.persistence.dao.IndexImageDao;
import org.asociacion.backend.persistence.dao.IndexPropertiesDao;
import org.asociacion.backend.persistence.dao.IndexTextDao;
import org.asociacion.backend.persistence.entities.TblIndexImage;
import org.asociacion.backend.persistence.entities.TblIndexProperties;
import org.asociacion.backend.persistence.entities.TblIndexText;
import org.asociacion.backend.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexServiceImpl implements IndexService {
	
	//private Logger log = LoggerFactory.getLogger(getClass());	
	
	@Autowired
	private IndexPropertiesDao indexPropertiesDao;
	
	@Autowired
	private IndexImageDao indexImageDao;
	
	@Autowired
	private IndexTextDao indexTextDao; 

	@Override
	@Transactional
	public Properties getProperties() {
		Properties properties = new Properties();
		for (TblIndexProperties indexProperties : indexPropertiesDao.list()) {
			properties.put(indexProperties.getKey(), indexProperties.getValue());
		}
		return properties;
	}
	
	@Override
	@Transactional
	public Properties getPropertiesText() {
		Properties properties = new Properties();
		for (TblIndexText tblIndexText : indexTextDao.list()) {
			properties.put(tblIndexText.getKey(), tblIndexText.getText());
		}
		return properties;
	}
	
	@Override
	@Transactional
	public List<String> getListImageName(String folder, boolean pattern) {
		List<String> listIndexImageBo = new LinkedList<String>();
		List<TblIndexImage> list = indexImageDao.getListImage(folder, pattern);
		for (TblIndexImage image : list) {
			listIndexImageBo.add(image.getName());
		}
		return listIndexImageBo;
	}

	@Override
	@Transactional
	public IndexImageBo getImage(String folder, String name) {
		TblIndexImage tblIndexImage = indexImageDao.getImage(folder, name);
		if ( tblIndexImage == null) {
			return null;
		}
		return new IndexImageBo(tblIndexImage);
	}

	public void setIndexPropertiesDao(IndexPropertiesDao indexPropertiesDao) {
		this.indexPropertiesDao = indexPropertiesDao;
	}

	public void setIndexImageDao(IndexImageDao indexImageDao) {
		this.indexImageDao = indexImageDao;
	}

	public void setIndexTextDao(IndexTextDao indexTextDao) {
		this.indexTextDao = indexTextDao;
	}

}
